#include "yocto/utest/run.hpp"

#include "core/cell-dyn.hpp"
#include "core/ljc.hpp"
#include "core/fcc.hpp"

#include "yocto/wtime.hpp"
#include "yocto/ios/ocstream.hpp"

using namespace FastMD;

YOCTO_UNIT_TEST_IMPL(cell_dyn)
{
	std::cerr << "Testing Dynamics" << std::endl;
	LJCutOff     potential(1,1,2.5);
	wtime        chrono;
	const size_t niter = 10;
	bool         append = false;
	const bool   isothermal = false;
	
	for( size_t m=1; m <= 14; ++m )
	{
		const U3D    cr(m,m,m);
		const size_t np = 4 * cr.items();
		const Real   L  = __pow( np, __ONE/__THREE );
		Frame F(3,np);
		std::cerr << "m=" << m << ", np=" << np << ", L=" << L << std::endl;
		F.set_box( V3D(L,L,L) );
		CellDynamics   dyn(F,2.8);
		FCC::Build( F[0], cr, F.box, F.ibox, 0.0 );
		double tmx = 0;
		for( size_t i=0; i < niter; ++i )
		{
			chrono.start();
			dyn.eval( F, potential, isothermal );
			tmx += chrono.query();
		}
		tmx /= niter;
		std::cerr << "\tV/pt= " << F.V/np << ", time= " << tmx*1000.0 << " ms" << std::endl;
		ios::ocstream fp( "tmx-cell-st.dat",append);
		append = true;
		fp("%u %.5e %.5e\n", unsigned(m), tmx, F.V/np );
	}
	
}
YOCTO_UNIT_TEST_DONE()

#include "core/cell-dyn-mt.hpp"

YOCTO_UNIT_TEST_IMPL(cell_dynMT)
{
	std::cerr << "Testing Dynamics" << std::endl;
	LJCutOff     potential(1,1,2.5);
	wtime        chrono;
	const size_t niter = 10;
	bool         append = false;
	const bool   isothermal = false;

	for( size_t m=1; m <= 14; ++m )
	{
		const U3D    cr(m,m,m);
		const size_t np = 4 * cr.items();
		const Real   L  = __pow( np, __ONE/__THREE );
		Frame F(3,np);
		std::cerr << "m=" << m << ", np=" << np << ", L=" << L << std::endl;
		F.set_box( V3D(L,L,L) );
		CellDynamicsMT dyn(F,2.8);
		FCC::Build( F[0], cr, F.box, F.ibox, 0.0 );
		double tmx = 0;
		for( size_t i=0; i < niter; ++i )
		{
			chrono.start();
			dyn.eval( F, potential, isothermal );
			tmx += chrono.query();
		}
		tmx /= niter;
		std::cerr << "\tV/pt= " << F.V/np << ", time= " << tmx*1000.0 << " ms" << std::endl;
		ios::ocstream fp( vformat("tmx-cell-np%u.dat", unsigned(dyn.num_threads)),append);
		append = true;
		fp("%u %.5e %.5e\n", unsigned(m), tmx, F.V/np );
	}
	
}
YOCTO_UNIT_TEST_DONE()
